package org.example.demo3.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;

/**
 * 商品成色枚举类
 * 作者:xuSen
 * 日期2025/3/13 22:42
 */
@Getter
@AllArgsConstructor
public enum ProductConditionEnum {
    ALMOST_NEW(0, "几乎全新"),
    SLIGHTLY_USED(1, "轻微使用"),
    MODERATELY_USED(2, "中度使用"),
    HEAVILY_USED(3, "重度使用");

    @EnumValue // 标记存储到数据库的值
    private final int code;
    private final String value;

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static ProductConditionEnum fromCode(int code) {
        return Arrays.stream(values())
                .filter(e -> e.code == code)
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("无效的成色码"));
    }

    // 根据中文值获取枚举
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static ProductConditionEnum fromValue(String value) {
        return Arrays.stream(values())
                .filter(e -> e.value.equals(value))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("无效成色值"));

    }}
